<?php


namespace App\Http\Controllers\Front;


use App\Http\Controllers\Controller;
use App\Model\ArticleCategories;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Swagger\Annotations as SWG;

class CategoriesController extends Controller
{
    /**
     * @SWG\Get(
     *     path="/front/index/user_category",
     *     tags={"用户相关"},
     *     summary="首页用户自定义分类",
     *     description="首页用户自定义分类",
     *     produces={"application/json"},
     *     @SWG\Parameter(ref="#/parameters/auth"),
     *     @SWG\Response(
     *          response="200",
     *          description="调用成功",
     *          @SWG\Schema(
     *              @SWG\Property(
     *                  property="set",
     *                  description="设置过的分类",
     *                  type="array",
     *                  @SWG\Items(
     *                      @SWG\Property(property="id", type="integer", description="分类id"),
     *                      @SWG\Property(property="title", type="string", description="分类名"),
     *                      @SWG\Property(property="is_modify", type="integer", description="是否允许被移除"),
     *                  )
     *              ),
     *              @SWG\Property(
     *                  property="unset",
     *                  description="未设置的分类",
     *                  type="array",
     *                  @SWG\Items(
     *                      @SWG\Property(property="id", type="integer", description="分类id"),
     *                      @SWG\Property(property="title", type="string", description="分类名"),
     *                      @SWG\Property(property="is_modify", type="integer", description="是否允许被移除"),
     *                  )
     *              )
     *          )
     *      )
     * )
     */
    public function userCategory()
    {
        $uc = DB::table("user_category")->where("user_id", Auth::id())->first();
        if (empty($uc)){
            $data = ArticleCategories::select('id', 'title', 'sort', 'parent_id')->where("is_enabled",0)->orderBy('sort')->get();
            $id = collect($data)->pluck('id');
            $id = implode(',', $id->toArray());
            DB::table('user_category')->updateOrInsert(['user_id'=>Auth::id()],['category'=>$id, 'created_at'=>Carbon::now()]);
            return $this->renderJson([
                'set' => [],
                'unset' => $data
            ]);
        }
        $ids = explode(',', $uc->category);
        $cate = ArticleCategories::select('id', 'title', 'is_modify')->where('is_enabled', 0)->orderBy('is_modify')->orderBy('sort')->whereIn('id', $ids)->get();
        $un = ArticleCategories::select('id', 'title', 'is_modify')->where('is_enabled', 0)->orderBy('is_modify')->orderBy('sort')->whereNotIn('id', $ids)->get();
        return $this->renderJson([
            'set' => $cate,
            'unset' => $un
        ]);
    }

    /**
     * @SWG\Post(
     *     path="/front/index/user_category",
     *     tags={"用户相关"},
     *     summary="添加自定义分类",
     *     description="添加自定义分类",
     *     consumes={"application/x-www-form-urlencoded"},
     *     produces={"application/json"},
     *     @SWG\Parameter(ref="#/parameters/auth"),
     *     @SWG\Parameter(name="category", in="formData", required=true, type="string", description="分类id数组"),
     *     @SWG\Response(
     *          response="200",
     *          description="操作成功"
     *      )
     * )
     */
    public function addUserCategory(Request $request)
    {
        $cate = $request->input('category');
        if (request('device') == 'android') {
            $cate = explode(',', request('category'));
        }
        $modify = ArticleCategories::where('is_modify',0)->pluck('id');
        foreach ($modify as $k=>$m){
            $cate[] = $m;
        }
        $cate = array_unique($cate);
        $id = ArticleCategories::whereIn('id', $cate)->where('is_enabled', 0)->pluck('id');
        $id = implode(',', $id->toArray());
        DB::table('user_category')->updateOrInsert(['user_id'=>Auth::id()],['category'=>$id, 'created_at'=>Carbon::now()]);
        return $this->success();
    }

    /**
     * @SWG\Post(
     *     path="/front/index/advise_category",
     *     tags={"用户相关"},
     *     summary="添加推荐分类",
     *     description="添加推荐分类",
     *     consumes={"application/x-www-form-urlencoded"},
     *     produces={"application/json"},
     *     @SWG\Parameter(ref="#/parameters/auth"),
     *     @SWG\Parameter(name="category", in="formData", required=true, type="string", description="分类名称"),
     *     @SWG\Response(
     *          response="200",
     *          description="操作成功"
     *      )
     * )
     */
    public function addAdviseCategory(Request $request)
    {
        $cate = $request->input('category');
        if (DB::table('suggest_category_name')->where('user_id', Auth::id())->where('category_name',$cate)->first()) {
            return $this->fail('你已经提议过了');
        }
        DB::table('suggest_category_name')->insert(['user_id'=>Auth::id(),'category_name'=>$cate]);
        return $this->success();
    }
}